﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace furion.blog.Domain.Query.Dept
{
    public class DeptDetailQuery
    {

        public long? DeptId { get; set; } = null;
        public long? DeptIdNE { get; set; } = null;
        public long? ParentId { get; set; } = null;
        public long? ParentIdNE { get; set; } = null;


        /***************可以进行模糊查询条件*************************/
        public string DeptName { get; set; } = string.Empty;
        public string DeptEn { get; set; } = string.Empty;

        public string Leader { get; set; } = string.Empty;
        public string Phone { get; set; } = string.Empty;

        public string IsDelete { get; set; } = string.Empty;

        /// <summary>
        /// 模糊查询
        /// </summary>
        public bool FuzzyQuery { get; set; } = false;

        /// <summary>
        /// 查询表达式
        /// </summary>
        /// <returns></returns>
        public Expression<Func<furion.blog.Domain.MySQL.Dept, bool>> Exp()
        {
            var exp = Expressionable.Create<furion.blog.Domain.MySQL.Dept>();
            exp.AndIF(DeptId != null, t => t.DeptId == DeptId);
            exp.AndIF(ParentId != null, t => t.ParentId == ParentId);
            exp.AndIF(DeptIdNE != null, t => t.DeptId != DeptIdNE);
            exp.AndIF(ParentIdNE != null, t => t.ParentId != ParentIdNE);
            exp.AndIF(!string.IsNullOrWhiteSpace(IsDelete),t=>t.IsDelete == IsDelete);
            if (!FuzzyQuery)
            {
                //全匹配查询
                exp.AndIF(!string.IsNullOrWhiteSpace(DeptName),t=>t.DeptName == DeptName);
                exp.AndIF(!string.IsNullOrWhiteSpace(DeptEn),t=>t.DeptEn == DeptEn);
                exp.AndIF(!string.IsNullOrWhiteSpace(Leader),t=>t.Leader == Leader);
                exp.AndIF(!string.IsNullOrWhiteSpace(Phone),t=>t.Phone == Phone);
            }
            else
            {
                //模糊查询
                exp.AndIF(!string.IsNullOrWhiteSpace(DeptName), t => t.DeptName.Contains( DeptName));
                exp.AndIF(!string.IsNullOrWhiteSpace(DeptEn), t => t.DeptEn.Contains(DeptEn));
                exp.AndIF(!string.IsNullOrWhiteSpace(Leader), t => t.Leader.Contains(Leader));
                exp.AndIF(!string.IsNullOrWhiteSpace(Phone), t => t.Phone.Contains(Phone));
            }
            return exp.ToExpression();
        }
    }
}
